/**
 * User控制器模块
 * controller\user.js
 *
 */

const express = require("express");

const util = require("../common/util");
/**
 * @typedef {UserDB}
 */
const UserDB = require("../model/sqlite/user");

/**
 * 获取用户信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function find(req, res) {
  (async function () {
    let db = UserDB.getInstance();
    await db.connect();
    let result = await db.find(req.params.id); //await db.close();
    util.logFormat(`获取【${req.params.id}】用户信息%O`, result);
    res.json(
      util.FormatJSONData(200, `获取用户信息【${req.params.id}】`, result)
    );
  })();
}

/**
 * 获取用户信息列表
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function findAll(req, res) {
  (async function () {
    //1.处理输入的数据
    //FIXME: 输入数据的正确性校验，一致性校验，完整性校验
    let limit = req.query.limit ? req.query.limit : -1;
    let offset = req.query.offset ? req.query.offset : -1; //2. 访问数据
    let db = UserDB.getInstance();
    await db.connect();
    let result = await db.findAll(limit, offset); //await db.close(); //3.输出数据
    util.logFormat(`获取【limit=${limit}&offset=${offset}】用户信息%O`, result);
    res.json(util.FormatJSONData(200, `获取用户信息列表`, result));
  })();
}

/**
 * 新增用户信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function add(req, res) {
  (async function () {
    //FIXME:req.body的数据安全性校验
    let user = {
      username: req.body.username,
      password: util.md5pwd(req.body.password),
      nickname: req.body.nickname,
      truename: req.body.truename,
      avatar: req.body.avatar,
      role: req.body.role,
      lastLoginTime: 0,
      lastLoginIP: "never login",
      createdTime: Date.now(),
      createdIP: util.getReqRemoteIP(req),
      updatedTime: Date.now(),
    };
    let db = UserDB.getInstance();
    await db.connect(); //FIXME:插入数据前，考虑校验是否已存在ISBN
    let result = await db.add(user); //await db.close();
    util.log(`新增用户信息：lastID->${result}`);
    res.json(util.FormatJSONData(201, `新增用户信息`, { lastID: result }));
  })();
}

/**
 * 更新用户信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function update(req, res) {
  (async function () {
    //FIXME:req.body的数据安全性校验
    let user = {
      id: req.params.id,
      username: req.body.username,
      password: util.md5pwd(req.body.password),
      nickname: req.body.nickname,
      truename: req.body.truename,
      avatar: req.body.avatar,
      role: req.body.role,
      updatedTime: Date.now(),
    };

    let db = UserDB.getInstance();
    await db.connect();
    let result = await db.update(user); //await db.close();
    util.log(`更新用户信息：changes->${result}`);
    res.json(util.FormatJSONData(200, `更新用户信息`, { changes: result }));
  })();
}

/**
 * 删除用户信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function remove(req, res) {
  (async function () {
    //FIXME:数据合法性校验
    let userId = req.params.id;
    let db = UserDB.getInstance();
    await db.connect();
    let result = await db.remove(userId); //await db.close();
    util.log(`删除用户信息：changes->${result}`);
    res.json(util.FormatJSONData(204, `删除用户信息`, { changes: result }));
  })();
}

module.exports = {
  find,
  findAll,
  add,
  update,
  remove,
  // getCount,
};
